

#include "MyMenu.h"
#include "MyButton.h"
#include "MyDefinitions.h"
#include "MyUniversal.h"
#include "MyEngine.h"

MyMenu::MyMenu(MyEngine* _this){notYetInited = true; parent = _this;}
MyMenu::~MyMenu(){
	clean_menu();
	#ifdef _DEBUG
	printf("destroyed menu\n");
#endif
}
void MyMenu::init_menu()
{
	set_clips();
    load_imgs();
	//clear the list for pointer holding
	buttons = std::list<MyButton>();
	//init button objects
	MyButton* temp_button = new MyButton (make_rect((SCREEN_W/2)-50,130,100,40),1,this);
	temp_button->set_clip(&clips[temp_button->get_id()*2-1]);
	buttons.push_back(*temp_button);
	temp_button = new MyButton (make_rect((SCREEN_W/2)-50,190,100,40),2,this);
	temp_button->set_clip(&clips[temp_button->get_id()*2-1]);
	buttons.push_back(*temp_button);
	temp_button = new MyButton (make_rect((SCREEN_W/2)-50,250,100,40),3,this);
	temp_button->set_clip(&clips[temp_button->get_id()*2-1]);
	buttons.push_back(*temp_button);
	
	//safety init off
	notYetInited = false;
	//display the menu
	display_menu();
}
void MyMenu::set_clips()
{
	//Clip the sprite sheet - NEW GAME 
	clips[CLIP_NEW].x = 0;
    clips[CLIP_NEW].y = 1;
    clips[CLIP_NEW].w = 100;
    clips[CLIP_NEW].h = 39;

    clips[CLIP_NEW_M].x = 0;
    clips[CLIP_NEW_M].y = 41;
    clips[CLIP_NEW_M].w = 100;
    clips[CLIP_NEW_M].h = 39;

	//Clip the sprite sheet - LOAD GAME 
    clips[CLIP_LOAD].x = 0;
    clips[CLIP_LOAD].y = 81;
    clips[CLIP_LOAD].w = 100;
    clips[CLIP_LOAD].h = 39;
    
    clips[CLIP_LOAD_M].x = 0;
    clips[CLIP_LOAD_M].y = 121;
    clips[CLIP_LOAD_M].w = 100;
    clips[CLIP_LOAD_M].h = 39;

	//Clip the sprite sheet - EXIT
	clips[CLIP_EXIT].x = 0;
    clips[CLIP_EXIT].y = 161;
    clips[CLIP_EXIT].w = 100;
    clips[CLIP_EXIT].h = 39;

	clips[CLIP_EXIT_M].x = 0;
    clips[CLIP_EXIT_M].y = 201;
    clips[CLIP_EXIT_M].w = 100;
    clips[CLIP_EXIT_M].h = 39;  	
}

void MyMenu::load_imgs()
{
	//Load the button sprite sheet
	#ifdef _WIN32
    buttonSheet = load_image( "misc\\buttons.png" );
	menuSheet = load_image("misc\\menu.jpg");
	#else
	menuSheet = load_image("misc/menu.jpg");
    buttonSheet = load_image( "misc/buttons.png" );
	#endif
    
    //If there was a problem in loading the button sprite sheet
    if((!buttonSheet)||(!menuSheet)) 
	{ 
		printf("ERROR:Cannot load menu images! (menu.jpg or buttons.png missing?)");
		parent->goto_gs(ES_QUIT); 
	}	
}
void MyMenu::clean_menu()
{
	unload_surface(menuSheet);
	unload_surface(buttonSheet);
	buttons.clear();
}
void MyMenu::display_menu()
{	
	//safety init
	if (notYetInited) init_menu();

	//display the background image
	SDL_BlitSurface(menuSheet,0,parent->screen,0);

	std::list<MyButton>::iterator i;
	for (i = buttons.begin();i != buttons.end();)
	{
		//dislay the current button
		i->show();
		++i;	
	}
	//Update the screen
	parent->redraw();
}
void MyMenu::process_event(SDL_Event& handle_event)
{
	//safety init
	if (notYetInited) init_menu();

	int x = handle_event.button.x; //The mouse x offset
	int y = handle_event.button.y; //The mouse y offset
	
	//process the incomming event
	switch (handle_event.type)
	{
	//if key was pressed
	case SDL_KEYDOWN:{	
		switch (handle_event.key.keysym.sym)
		{
			case SDLK_F2: 
				{
					parent->kill_game();
					parent->goto_gs(ES_GAME); 
					break;
				}
			case SDLK_ESCAPE:
				{
					
					parent->goto_gs(ES_QUIT); 
					break;
				}
		}	
	}
	//Mouse moved - check the position over buttons
	case SDL_MOUSEMOTION:		{
		std::list<MyButton>::iterator i;
		//go through buttons
		for (i = buttons.begin();i != buttons.end();)
		{
			//load game not implemented
			if (i->get_id() == 2) i++;
		//mouse over some button
			if(i->in_me(x,y)) 
			{
				i->set_clip(&clips[i->get_id()*2]);	//Set the button mouse-over sprite		
				i->show();							//show the button
			}
			else 
			{
				i->set_clip(&clips[i->get_id()*2-1]);	//Set the button mouse-out sprite            
				i->show();
			}
			++i;
		}
		break;
		}	
	//Mouse button pressed
	case SDL_MOUSEBUTTONDOWN:
	{	//Left mouse button
		if(handle_event.button.button==SDL_BUTTON_LEFT)
		{   
			//TODO: sound?       
			//check whether the mouse is over some button            			
			std::list<MyButton>::iterator i;
			//go through buttons
			for (i = buttons.begin();i != buttons.end();)
			{
				//mouse over some button
				if(i->in_me(x,y)) 
				{
					switch(i->get_id())
						{
							case 1: {parent->goto_gs(ES_GAME); return;}
							case 2: {/*TODO: load saved game!! */ return;}
							case 3: {parent->goto_gs(ES_QUIT); return;}
							default: {}
						}
				}
				else ++i;
			}
		}
	break;
	}
	//If a mouse button was released
	case SDL_MOUSEBUTTONUP:		{ /* TODO: sound?*/ }
	 }
}


	